package views.inauguralmanage;

import org.intellij.lang.annotations.Language;
import utils.DBUtil;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class Allnaugural extends JFrame {

    private JScrollPane jsp = new JScrollPane();
    private JTable table = new JTable();
    private MyModel model;

    public Allnaugural() {
        setTitle("显示所有聘用情况");
        setSize(800, 800);
        setResizable(true); // 不可改变窗口大小
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);

        setLocationRelativeTo(null);

        model = new MyModel();
        table.setModel(model);


        table.getColumnModel().getColumn(0).setHeaderValue("职业名称");
        table.getColumnModel().getColumn(1).setHeaderValue("职业类别");
        table.getColumnModel().getColumn(2).setHeaderValue("企业");
        table.getColumnModel().getColumn(3).setHeaderValue("求职者身份证");
        table.getColumnModel().getColumn(4).setHeaderValue("求职者名称");
        table.getColumnModel().getColumn(5).setHeaderValue("性别");
        table.getColumnModel().getColumn(6).setHeaderValue("年龄");
        table.getColumnModel().getColumn(7).setHeaderValue("联系电话");
        DefaultTableCellRenderer r = new DefaultTableCellRenderer();
        r.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, r);

        // 设置可见视图的接口
        jsp.setViewportView(table);
        // 定义表格 宽500，高度400
        jsp.setPreferredSize(new Dimension(500, 500));
        // 设置横向和垂直滚动条可见
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        this.setLayout(new BorderLayout());
        this.add(jsp);
    }

    class MyModel extends AbstractTableModel {
        private int row;
        private int column;
        private ResultSet rs;
        private Statement stmt;
        @Language("MySQL")
        private String sql = "select job_name,category_name,employer_name, identityNum,name,gender,age,e.contact_phone from job_match" +
                " LEFT JOIN job_info ji on job_match.job_id = ji.job_id " +
                "LEFT JOIN job_seeker js on js.seeker_id = job_match.seeker_id " +
                "LEFT JOIN job_category jc on jc.category_id = ji.category_id" +
                " LEFT JOIN employer e on ji.employer_id = e.employer_id";

        public MyModel() {
            stmt = DBUtil.getStatement();
            try {
                rs = stmt.executeQuery(sql);
                rs.last();// 将光标移到最后一行
                row = rs.getRow();// 获取行号(最大行索引)
                ResultSetMetaData rsmd = rs.getMetaData();// 通过结果集对象来获取
                column = rsmd.getColumnCount();// 获取列数
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getColumnCount() {
            return column;
        }

        @Override
        public int getRowCount() {
            return row;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Object value = null;
            try {
                rs.absolute(rowIndex + 1);
                value = rs.getString(columnIndex + 1);// 获取表里的数据
            } catch (Exception e) {
                e.printStackTrace();
            }
            return value;
        }
    }

    public static void main(String[] args) {
        new Allnaugural().setVisible(true);
    }
}
